/**
 * 
 */
package com.datastructure.tree.rtree;

/**
 * @author Jun
 *
 */
public class MBR implements BaseGeo {

	// left Up Point
	private Point2D leftUpPoint;
	
	// right down Pont
	private Point2D rdPoint;	

	public Point2D getLeftUpPoint() {
		return leftUpPoint;
	}

	public void setLeftUpPoint(Point2D leftUpPoint) {
		this.leftUpPoint = leftUpPoint;
	}

	public Point2D getRdPoint() {
		return rdPoint;
	}

	public void setRdPoint(Point2D rdPoint) {
		this.rdPoint = rdPoint;
	}

	public MBR(Point2D lup, Point2D rdp){
		
		this.leftUpPoint = lup;
		this.rdPoint = rdp;
		
	}
	// java.awt.Rectangle:
	//			public boolean intersects(Rectangle r) 
	public boolean intersect(BaseGeo obj){
		
		if(obj == null)
			return false;
		
		// MBR intersect
		if(obj instanceof MBR){
			
			MBR newmbr = (MBR)obj;
			
			boolean b1 = (leftUpPoint.getX()<= newmbr.leftUpPoint.getX()&& newmbr.leftUpPoint.getX()<= rdPoint.getX());
			boolean b2 = (leftUpPoint.getX()<= newmbr.rdPoint.getX()&& newmbr.rdPoint.getX()<= rdPoint.getX());
			
			boolean b3 = (leftUpPoint.getY()<= newmbr.leftUpPoint.getY()&& newmbr.leftUpPoint.getY()<= rdPoint.getY());
			boolean b4 = (leftUpPoint.getY()<= newmbr.rdPoint.getY()&& newmbr.rdPoint.getY()<= rdPoint.getY());
			
			return (b1||b2)&&(b3||b4);
		}
		
		// MBR intersect with Circle
		if( obj instanceof Circle){
			
			Circle c = (Circle) obj;
			
			double tmpf = 0;
			int counter = 0;
			double x = 0;
			double y = 0;
			double x0 = c.getCenter().getX();
			double y0 = c.getCenter().getY();
			double r = c.getRadius();
			
			double x1 = leftUpPoint.getX();
			double x2 = rdPoint.getX();
			double y1 = leftUpPoint.getY();
			double y2 = rdPoint.getY();
			
			x = x1;
			tmpf = r*r  - Math.pow((x - x0),2);
			if(tmpf >=0 ){				
				tmpf = Math.sqrt(tmpf);				
				y = y0 + tmpf;
				counter += (y>= y1 && y <= y2)?1:0;
				y = y0 - tmpf;
				counter += (y>= y1 && y <= y2)?1:0;					
			}  
			
			if(counter >0) return true;
			
			x = x2;
			tmpf = r*r  - Math.pow((x - x0),2);
			if(tmpf >=0 ){				
				tmpf = Math.sqrt(tmpf);				
				y = y0 + tmpf;
				counter += (y>= y1 && y <= y2)?1:0;
				y = y0 - tmpf;
				counter += (y>= y1 && y <= y2)?1:0;					
			}
			
			if(counter >0) return true;
			
			y = y1;
			tmpf = r*r  - Math.pow((y - y0),2);
			if(tmpf >=0 ){				
				tmpf = Math.sqrt(tmpf);				
				x = x0 + tmpf;
				counter += (x>= x1 && x <= x2)?1:0;
				x = x0 - tmpf;
				counter += (x>= x1 && x <= x2)?1:0;					
			}
			
			if(counter >0) return true;
			
			y = y2;
			tmpf = r*r  - Math.pow((y - y0),2);
			if(tmpf >=0 ){				
				tmpf = Math.sqrt(tmpf);				
				x = x0 + tmpf;
				counter += (x>= x1 && x <= x2)?1:0;
				x = x0 - tmpf;
				counter += (x>= x1 && x <= x2)?1:0;					
			}
			
			return (counter >0);
		}
		
		return false;
	}
	
	public boolean Contains(Point2D p){
		
		boolean b1 = (p.getX()>=leftUpPoint.getX())&&(p.getX()<=leftUpPoint.getX());
		boolean b2 = (p.getY()>=leftUpPoint.getY()&&(p.getY()<=rdPoint.getY()));
		
		return  b1&&b2;
	}
	
}






